package com.coderg.gmall.product.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.coderg.gmall.model.product.SkuInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * @description: SkuInfo
 * @author：coderG
 * @date: 2025/5/18
 */
@Mapper
public interface SkuInfoMapper extends BaseMapper<SkuInfo> {

    /**
     * sku上下架
     *
     * @param skuId
     * @param mark
     * @return
     */
    @Update("update `SkuInfo` set is_sale=#{mark} where id =#{skuId}")
    int updateSaleState(@Param("skuId") Long skuId, @Param("mark") Short mark);

    /**
     * 获取销售属性 和具体商品sku值的对应关系 键值对关系返回  sku-->sale_attr_value_id
     *
     * @param spuId
     * @return
     */
    @Select("SELECT sku_id," +
            "GROUP_CONCAT(DISTINCT sale_attr_value_id ORDER BY sale_attr_value_id ASC SEPARATOR ',' ) sale_attr_value_id " +
            "FROM sku_sale_attr_value t1 " +
            "WHERE t1.spu_id = 49 " +
            "GROUP BY t1.sku_id")
    List<Map> selectSkuAndSaleBySpuId(@Param("spuId") Long spuId);

    /**
     * 扣减库存
     *
     * @param skuId
     * @param num
     * @return
     */
    @Update("UPDATE `sku_info` SET stock= stock-#{num} WHERE stock>=#{num} AND id =#{skuId}")
    int updateStock(@Param("skuId") Long skuId, @Param("num") Integer num);

    /**
     * 回退库存
     *
     * @param skuId
     * @param num
     * @return
     */
    @Update("UPDATE `sku_info` SET stock= stock+#{num} WHERE id =#{skuId}")
    int addStock(@Param("skuId") Long skuId, @Param("num") Integer num);
}

